import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from '@/pages/Login.vue'

import Register from '@/pages/Register.vue'

import Profile from 'pages/Profile.vue'

import EditProfile from 'pages/EditProfile.vue'

import Follow from 'pages/Follow.vue'

import MyComments from 'pages/MyComments.vue'
import MyFavorite from 'pages/MyFavorite.vue'
import Home from 'pages/Home'
import PostDetail from 'pages/PostDetail.vue'
import TabManage from 'pages/TabManage.vue'
import Search from 'pages/Search.vue'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [

    { path: '/home', redirect: '/' },
    { path: '/', component: Home, name: 'home' },
    { path: '/login', component: Login, name: 'login' },
    { path: '/register', component: Register, name: 'register' },
    { path: '/profile', component: Profile, name: 'profile' },
    { path: '/edit-profile', component: EditProfile, name: 'edit-profile' },
    { path: '/follow', component: Follow, name: 'follow' },
    { path: '/my-comments', component: MyComments, name: 'my-comments' },
    { path: '/my-favorite', component: MyFavorite, name: 'my-favorite' },
    { path: '/tab-manage', component: TabManage, name: 'tab-manage' },
    { path: '/search', component: Search, name: 'search' },
    { path: '/post-detail/:id', component: PostDetail, name: 'post-detail' }
  ]
})

const AuthUrls = [
  '/peofile',
  '/edit-profile',
  '/follow',
  '/my-comments',
  '/my-favorite'
]

router.beforeEach((to, from, next) => {
  if (AuthUrls.includes(to.path)) {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})
export default router
